﻿using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Localization;

namespace Piedone.ContentTemplates.Services.TemplateLoaders
{
    public class BodyTemplateLoader : ITemplateLoader
    {
        private readonly ITemplateLoaderDescriptor _descriptor = new TemplateLoaderDescriptor("Body", new LocalizedString("Body"), new LocalizedString("Initializes the body text from the template.")); 
        public ITemplateLoaderDescriptor Descriptor { get { return _descriptor; } }

        public bool CanHandle(IContent item)
        {
            return item.Has<BodyPart>();
        }

        public void LoadFromTemplate(IContent template, IContent item)
        {
            if (!CanHandle(template) || !CanHandle(item)) return;

            item.As<BodyPart>().Text = template.As<BodyPart>().Text;
        }
    }
}